package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.req.DelayReceiveReqBean;
import com.ymatou.iapi.trading.operate.service.DelayReceiveCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.sqlwapper.DefaultWapper;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 延长收货时间 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_DelayReceive {

	private static DelayReceiveReqBean delayReceiveReqBean;
	private static DelayReceiveCall delayReceiveCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("延长收货时间");
	}

	@Before
	public void caseUp() {

		delayReceiveReqBean = new DelayReceiveReqBean();
		delayReceiveCall = new DelayReceiveCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(delayReceiveCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_DelayReceive_001() {
		Logger.start(true, "卖家延长收货时间成功");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			int delayDay = 1;
			delayReceiveReqBean.setDelayDay(delayDay);
			delayReceiveReqBean.setOrderId(orderId);
			delayReceiveReqBean.setOperateUserId(sellerId);
			delayReceiveReqBean.setRemark("自动化测试延长信息");
			delayReceiveReqBean.setOperateUserType(0);
			// 发送请求
			delayReceiveCall.setData(delayReceiveReqBean);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.SUCCESS, delayReceiveCall.getCode(),
					"验证Code");
			// 数据库验证
			DefaultWapper database = new DefaultWapper();
			Logger.verifyEquals(1, database.selectOrderFrozenByOrderId(orderId)
					.size(), "验证订单延长收货记录表");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_002() {
		Logger.start(false, "此订单不存在");
		try {
			int orderId = 1111;
			int sellerId = 288484;
			int delayDay = 1;
			delayReceiveReqBean.setDelayDay(delayDay);
			String delayTime = YMTDateUtil.getSysTime();
			delayReceiveReqBean.setOrderId(orderId);
			delayReceiveReqBean.setOperateUserId(sellerId);
			// delayReceiveReqBean.setMemo("自动化测试延长信息");
			// 发送请求
			delayReceiveCall.setData(delayReceiveReqBean);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.FAILED201, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains(ECode.FAILED201_MSG_11,
					delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_003() {
		Logger.start(true, "当前订单还没有超过可再次延期的时间限制");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			Integer orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额		
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			int delayDay = 1;
			delayReceiveReqBean.setDelayDay(delayDay);
			String delayTime = YMTDateUtil.getSysTime();
			delayReceiveReqBean.setOrderId(orderId);
			delayReceiveReqBean.setOperateUserId(sellerId);
			// delayReceiveReqBean.setMemo("自动化测试延长信息");
			// 发送请求
			delayReceiveCall.setData(delayReceiveReqBean);
			delayReceiveCall.callService();
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.FAILED201, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyEquals("哈尼，每次延期操作间隔必须大于24小时",
					delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_004() {
		Logger.start(false, "订单号为null");
		try {
			int orderId = 1234;
			int sellerId = 288484;
			int delayDay = 1;
			delayReceiveReqBean.setDelayDay(delayDay);
			String delayTime = YMTDateUtil.getSysTime();
			//delayReceiveReqBean.setOrderId(orderId);
			delayReceiveReqBean.setOperateUserId(sellerId);
			delayReceiveReqBean.setRemark("自动化测试延长信息");
			// 发送请求
			delayReceiveCall.setData(delayReceiveReqBean);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.ERROR400, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains(ECode.ERROR400_MSG_9,
					delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_005() {
		Logger.start(false, "卖家id为null");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额	
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			// 商家发货
			new OrderCallService().dispatched(orderId, sellerId);
			Logger.comment("前置数据准备结束");
			int delayDay = 1;
			delayReceiveReqBean.setDelayDay(delayDay);
			String delayTime = YMTDateUtil.getSysTime();
			delayReceiveReqBean.setOrderId(orderId);
			//delayReceiveReqBean.setOperateUserId(sellerId);
			// delayReceiveReqBean.setMemo("自动化测试延长信息");
			// 发送请求
			delayReceiveCall.setData(delayReceiveReqBean);
			delayReceiveCall.callService();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_006() {
		Logger.start(false, "延长的时间为0");
		try {
			int orderId = 1234;
			int sellerId = 288484;
			int delayDay = 0;
			delayReceiveReqBean.setDelayDay(delayDay);
			String delayTime = YMTDateUtil.getSysTime();
			delayReceiveReqBean.setOrderId(orderId);
			delayReceiveReqBean.setOperateUserId(sellerId);
			delayReceiveReqBean.setRemark("自动化测试延长信息");
			// 发送请求
			delayReceiveCall.setData(delayReceiveReqBean);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.ERROR400, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains("必须大于0", delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_007() {
		Logger.start(false, "延长的时间小于0");
		try {
			int orderId = 1234;
			int sellerId = 288484;
			int delayDay = -1;
			delayReceiveReqBean.setDelayDay(delayDay);
			String delayTime = YMTDateUtil.getSysTime();
			delayReceiveReqBean.setOrderId(orderId);
			delayReceiveReqBean.setOperateUserId(sellerId);
			delayReceiveReqBean.setRemark("自动化测试延长信息");
			// 发送请求
			delayReceiveCall.setData(delayReceiveReqBean);
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.ERROR400, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains("必须大于0", delayReceiveCall.getMsg(), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_DelayReceive_008() {
		Logger.start(false, "当前订单状态不是已发货,延长收货失败");
		try {
			// 创建订单
			Logger.comment("前置数据准备");
			PlaceOrderResp placeOrderResp = new OrderCallService()
					.createM2cOrder();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额
			// 支付订单
			new OrderCallService().orderPayRequest(userId, totalPrice,
					tradingId);
			// 接单
			new OrderCallService().acceptOrder(orderId, sellerId);
			/*	            // 商家发货
				            new OrderCallService().dispatched(orderId, sellerId);*/
			Logger.comment("前置数据准备结束");
			int delayDay = 1;
			delayReceiveReqBean.setDelayDay(delayDay);
			String delayTime = YMTDateUtil.getSysTime();
			delayReceiveReqBean.setOrderId(orderId);
			delayReceiveReqBean.setOperateUserId(sellerId);
			delayReceiveReqBean.setRemark("自动化测试延长信息");
			// 发送请求
			delayReceiveCall.setData(delayReceiveReqBean);
			delayReceiveCall.callService();
			delayReceiveCall.callService();
			Logger.verifyEquals(ECode.FAILED201, delayReceiveCall.getCode(),
					"验证Code");
			Logger.verifyIsContains("未发货,不能做延长收货处理", delayReceiveCall.getMsg(),
					"验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}